package middleware

import (
	"server/global"
	"server/myutils"

	"github.com/gin-gonic/gin"
)

func RouteRole(minrole int) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		role, exists := ctx.Get("role")
		if !exists {
			myutils.ResultJSON(ctx, global.ERROR_TOKEN, nil)
			ctx.Abort()
		}
		//类型断言,确定是这种类型防止编译报错
		if role.(int) < minrole {
			myutils.ResultJSON(ctx, global.INSUFFICIENt_PERMISSION, nil)
			//给日志系统报错用
			ctx.Abort()
		}
		ctx.Next()
	}
}
